﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DemoPersonalizeSearch
{
    public partial class TagClouds : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string format = "<span id='{0}' class='wrd tagcloud{0}'><a href='#tagcloud'>{1}</a></span> ";

            if (!IsPostBack)
            {
                DeliciousDatabaseDataContext db = new DeliciousDatabaseDataContext();
                if (Session.IsLogin())
                {
                    UserData ud = db.UserDatas.Where(x => x.Username == Session.UserLogin()).FirstOrDefault();
                    UserItem ui = new UserItem();
                    ui.ParseUserData(ud);
                    List<TagItem> list = ui.Tags.OrderByDescending(x => x.Weight).Take(50).OrderBy(x => x.TagName).ToList();
                    double max = list.Max(x => x.Weight);
                    double min = list.Min(x => x.Weight);

                    string str = "";
                    str += @"<div id='htmltagcloud'>";
                    int i = 0;
                    foreach (var item in list)
                    {
                        i = ((int)((item.Weight - min)*10 / (max - min))+1);
                        if (i > 10) i = 10;
                        
                        
                        str += String.Format(format, i, item.TagName);
                    }


                    str += "</div>";
                    HtmlEncode.Text = str;
                }
                else {
                    HtmlEncode.Text = "User not Login!";
                }
            }
        }
    }
}